
package com.gs.netty.client;


import com.gs.netty.support.LoadBalance;
import org.springframework.util.CollectionUtils;

import java.util.List;

public abstract class AbstractLoadBalance<T> implements LoadBalance<T> {


    @Override
    public T  loadBalance(List<T> invokerChannels) {

        if (CollectionUtils.isEmpty(invokerChannels)) {
            return null;
        }

        // 只有一个 服务提供者
        if (invokerChannels.size() == 1 && isAvailable(invokerChannels.get(0))) {
            return invokerChannels.get(0);
        }
        // 负载均衡
        return doLoadBalance(invokerChannels);
    }

    protected abstract T  doLoadBalance(List<T> channelWrappers);

    protected boolean isAvailable(T channelWrapper) {
        return true;
    }

}
